package com.andy.nature.vm

import androidx.lifecycle.MutableLiveData
import com.andy.base.utils.MVUtils
import com.andy.common.base.BaseViewModel
import com.andy.common.net.HttpRepository
import com.andy.common.model.Label
import com.andy.common.model.RecruitBean
import com.andy.common.model.Work
import com.andy.logcat.LogUtils

/**
 * Author:Andy
 * Date:2023/3/20 16:19
 * Description:RecruitViewModel
 */
class RecruitViewModel : BaseViewModel() {
    val workData = MutableLiveData<List<Work>>()
    var totalLabelList: MutableList<Label> = mutableListOf()
    var commonLabelData = MutableLiveData<List<Label>>()
    var createLiveData = MutableLiveData<Boolean>()

    companion object {
        const val LABEL_KEY = "common_label"
    }

    init {
        val lastData = MVUtils.getParcelable(LABEL_KEY, RecruitBean::class.java)
        val commonLabelList = lastData?.labels
        if (!commonLabelList.isNullOrEmpty()) {
            commonLabelList.apply {
                commonLabelData.postValue(this)
            }
        } else {
            commonLabelData.postValue(emptyList())
        }
    }

    /**
     * 获取招聘列表
     */
    fun getRecruitList(keyword: String? = "", ids: List<String>? = ArrayList(), index: Int? = 0, is_except: Int? = 0, cb: ((groupCount: Int) -> Unit)? = null) {
        launchUI {
            val response = HttpRepository.retrofit.getRecruitList(keyword, ids, index, 1, is_except)
            val workList = response.body()?.works
            if (response.isSuccessful) {
                workList?.let {
                    response.body()?.labels?.apply {
                        totalLabelList.clear()
                        totalLabelList.addAll(this)
                    }
                    val totalCount = it.size
                    cb?.apply {
                        var groupCount = if (totalCount % 8 == 0) {
                            totalCount / 8
                        } else {
                            totalCount / 8 + 1
                        }
                        if (groupCount == 0) {
                            groupCount = 1
                        }
                        invoke(groupCount)
                    }
                    workData.postValue(it)
                }
            }
        }
    }

    /**
     * 生成海报二维码
     */
    fun createQrcode(ids: List<String>? = ArrayList()) {
        launchUI {
            val response = HttpRepository.retrofit.createQrcode(ids, 1)
            if (response.isSuccessful) {
                createLiveData.postValue(true)
            } else {
                createLiveData.postValue(false)
            }
        }
    }

    /**
     * 设置为常用的标签
     */
    fun setCommonLabelList(labelList: List<Label>) {
        commonLabelData.postValue(labelList)
        saveData(labelList)
    }

    /**
     * 保存当前选择的数据
     */
    private fun saveData(labelList: List<Label>) {
        val data = RecruitBean()
        data.labels = labelList
        MVUtils.put(LABEL_KEY, data)
    }

}